<?php

/**
 * 
 */
 
class com extends Plugin
{
    //    internal variables
    
    //    Constructor
    function __construct ( ){
        parent::__construct();
        $this->scope[]   = 'Item_supprimer';   
        $this->scope[]   = 'Item_returnArray';
        $this->scope[]   = 'template';
        $this->droits    = 'VISITOR';
        $this->nom       = 'Commentaires';
        $this->className = 'com';
        $this->desc      = 'Permet d\'ajouter des commentaires sur chaque item.';
        $this->uniId     = 'ComUNIID';
        $this->version   = '0.3';
        //--- pagination + anti bot
    
        $sql     = SQL::GetInstance();
        $requete = 'SELECT * FROM '.$sql->getPrefix().'com ORDER BY dateCom DESC';
        $result  = $sql->doRequeteLignes($requete,0,10);
        $i = 0;
        $data = '';
        foreach($result as $d)
            $data .= '<tr class="mod'.($i++%2).'"><td style="width:25%;"><strong>'.$d['loginCom'].'</strong><br  /><small>('.trDateFr($d['dateCom']).')<br />[item n°'.$d['idItemCom'].']</small></td><td>'.$d['com'].'</td></tr>
';  
    
        $this->content   = 
            '<h2>Derniers commentaires</h2>
            <div class="tableCom">
                <table style="width: 99%; text-align: left;">
                    '.$data.'
                </table>
            </div>
            <p>
                <input class="bAnnuler" value="Fermer" type="button" />
            </p>';
        
        
    }    
    
    public function Item_supprimer($item)
    {
        $sql = SQL::GetInstance();
        $req = 'DELETE FROM '.$sql->getPrefix().'com WHERE idItemCom="'.$item->getIdItem().'"';
        $sql->executer($req);
    }
    
    public function Item_returnArray($item)
    {
        $sql = SQL::GetInstance();
        $requete = 'SELECT COUNT(*) AS nb FROM '.$sql->getPrefix().'com WHERE idItemCom = "'.$item->getIdItem().'"';
        $r = $sql->doRequeteLigne($requete);
        $r = $r['nb'];
        $item->setMore(array(
            'NB_COM_ITEM'   => $r,
            'LIEN_COM_ITEM' => 'PLUGIN=com&amp;idItem='.$item->getIdItem()));
    }

    public function action(){
        if(parent::action() == 'nop') return 'nop';
        if(!$this->ok()) return 'c_err1';
        
        global $rechercheGet, $listTagsGet, $listTypesGet, $IS_ADMIN, $trisGet;
        
        if(isset($_POST['idItem']) && isset($_POST['comValue'])){
            global $LOGIN;
            $com = nl2br(CleanVar($_POST['comValue']));
            $idC = (int)$_POST['idItem'];
            if(!empty($com)){
                $sql     = SQL::GetInstance();
                $idReq   = $sql->nextId('com');
                $requete = 'INSERT INTO '.$sql->getPrefix().'com(idCom,idItemCom,loginCom,com,dateCom) VALUES('.$idReq.', '.$idC.', \''.$LOGIN.'\', \''.$com.'\', SYSDATE())';
                $sql->executer($requete);
            }
            index('?'.$_POST['cleanParams'].'&PLUGIN=com&idItem='.$idC);
        }
        elseif(isset($_GET['supp']) && $IS_ADMIN && isset($_GET['idItem'])){
            $sql = SQL::GetInstance();            
            $id  = (int)$_GET['idItem'];
            $req = 'DELETE FROM '.$sql->getPrefix().'com WHERE idCom="'.(int)$_GET['supp'].'"';
            $sql->executer($req);
            index('?recherche='.$rechercheGet.'&tags='.$listTagsGet.'&types='.$listTypesGet.'&tris='.$trisGet.'&PLUGIN=com&idItem='.$id);
        }
        elseif(isset($_GET['idItem'])){
            global $CONF;
            $action = isset($_GET['action']) ? $_GET['action'] : null;
            $id     = (int)$_GET['idItem'];
            try{
                $item   = new Item($id);
                $isVide = $item->getIconeItem();
                $icone  = (!empty($isVide)) ? 
                    $item->getIconeItem() : $CONF['theme_dir'].'/icone/'.$item->getTypeItem().'.png';
                $this->content = 
                    '<form action="index.php" method="post">
                        <p>
                            <img src="'.$icone.'" alt="" />
                            <input type="hidden" name="PLUGIN" value="com" />
                            <input type="hidden" name="cleanParams" value="recherche='.$rechercheGet.'&tags='.$listTagsGet.'&types='.$listTypesGet.'&tris='.$trisGet.'" />
                            <input type="hidden" name="idItem" value="'.$id.'" />        
                            <label for="cp1">Votre commentaire pour l\'item : <br />"'.$item->getNom().'"<br />
                                <textarea id="cp1" name="comValue" rows="3" cols="50"></textarea>
                            </label>
                            <br />
                            <input type="submit" value="Ajouter le commentaire" />
                            <input class="bAnnuler" value="Fermer" type="button" />
                        </p>
                    </form>';          
                $sql     = SQL::GetInstance();
                $requete = 'SELECT * FROM '.$sql->getPrefix().'com WHERE idItemCom = "'.$id.'" ORDER BY dateCom DESC';
                $result  = $sql->doRequeteLignes($requete,0,30);
                $data    = '';
                if(count($result) == 0) $data .= '<small>(Aucun)</small><br />';
                $i = 0;
                global $IS_ADMIN;
                foreach($result as $d){
                    $admin = ($IS_ADMIN) ? '<br /><a href="?recherche='.$rechercheGet.'&tags='.$listTagsGet.'&types='.$listTypesGet.'&tris='.$trisGet.'&amp;PLUGIN=com&idItem='.$id.'&supp='.$d['idCom'].'" style="font-size: xx-small;">[supprimer]</a>' : '';
                    $data .= '<tr class="mod'.($i++%2).'"><td style="width:25%;"><strong>'.$d['loginCom'].'</strong><br  /><small>('.trDateFr($d['dateCom']).')</small>'.$admin.'</td><td>'.$d['com'].'</td></tr>
';
                }

                $this->content .=
                    '
                    <h2>Derniers commentaires sur cet item</h2>
                        <div class="tableCom">
                            <table style="width: 99%; text-align: left;">
                                '.$data.'
                            </table>
                        </div>
                    ';
          }
          catch(ListerException $e){}
        }
    }

}


?>
